from django.shortcuts import render
from django.http import JsonResponse
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync

# Create your views here.
def index(request):
    return render(request, "chat/index.html")

def room(request, room_name):
    print(dir(request))
    return render(request, "chat/room.html", {"room_name": room_name})

def pushRedis(request):
    # 消息推送接口调用方法： http://127.0.0.1:8000/chat/pushRedis?room=test， room 房间名
    room = request.GET.get('room')
    def push(msg):
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            room,
            {
                "type": "tui.song",
                'msg':msg
            }
        )
 
    push('推送测试！')
    return JsonResponse({'1':1})